import Vue from 'vue'
import Vuex from 'vuex'
import { getaip } from '../api/login'
Vue.use(Vuex)

export default new Vuex.Store({
  // 开启严格模式
  strict: true,
  state: {
    token: localStorage.getItem('token') || '',
    refreshtoken: localStorage.getItem('refreshtoken') || ''
  },
  mutations: {
    settoken (state, newtoken) {
      state.token = newtoken
      window.localStorage.setItem('token', newtoken)
    },
    setrefreshtoken (state, refreshtoken) {
      state.refreshtoken = refreshtoken
      window.localStorage.setItem('refresh_token', refreshtoken)
    }
  },
  actions: {
    async loginaction (store, objs) {
      // 发送登陆请求
      const rep = await getaip(objs)
      // 2.提交到mutation，同步到token和refreshtoken
      const { token, refresh_token: refreshToken } = rep.data.data
      store.commit('settoken', token)
      store.commit('setrefreshtoken', refreshToken)
    }
  },
  modules: {
  }
})
